// pages/login/login.js
import {ajax} from "../../../utils/httpRequest";
import {URI_USERSTATUS, URI_LOGINPAGE} from "../../../utils/api";

const util = require("../../../utils/util.js");

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    password: '',
    captcha: '',
    captchacode: '',
    ranNum: '',
    isChecked: false,
    show_no_dang_yuan: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    //调用工具类函数生成图形验证码
    var id = util.randomStr(4)
    this.setData({
      ranNum: id,
      captcha: util.verification(id)
    });

  },

  getTel: function (e) {
    this.setData({
      tel: e.detail.value
    });

    try {
      var value = wx.getStorageSync('user');
      var user = JSON.parse(value);
      if (value && user.tel == e.detail.value) {
        console.log(JSON.parse(value));
        this.setData({
          password: user.password
        })
      }
    } catch (e) {

    }

  },

  getPass: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  changeStatus: function () {
    this.setData({
      isChecked: !this.data.isChecked
    })
  },

  getCaptchaCode: function (e) {
    this.setData({
      captchacode: e.detail.value
    })
  },

  //点击验证码本身改变验证码
  changeCode: function () {
    var id = util.randomStr(4);
    this.setData({
      ranNum: id,
      captcha: util.verification(id)
    });
  },


  //已注册账户直接登录
  goToIndex() {
    var that = this;
    //验证手机号
    if (!this.data.tel) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      if (!util.validateTel(this.data.tel)) {
        wx.showToast({
          title: '手机号输入有误',
          icon: 'none',
          duration: 2000
        });
        return false;
      } else {
        ajax.post(URI_LOGINPAGE, {
          tel: this.data.tel,
          password: this.data.password,
          captcha: this.data.captchacode,
          id: this.data.ranNum
        }, {}, res => {
          //console.log(res.data);
          wx.setStorageSync('uid', res.data.uid);
          wx.setStorageSync('token', res.data.token);
          this.getUserStatus();
          //保存密码！
          var userinfo = null;
          if (this.data.isChecked) {
            userinfo = {
              tel: res.data.tel,
              password: this.data.password,
              token: res.data.token
            };
            try {
              wx.setStorageSync('user', JSON.stringify(userinfo))
            } catch (e) {

            }
          }
        });
      }
    }
  },

  //已注册用户忘记密码
  forgetPass: function () {
    if(!this.data.tel){
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (!util.validateTel(this.data.tel)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: '../reset/reset?tel=' + this.data.tel,
      })
    }
  },

  //未注册用户注册，用户注册
  goToRegist: function () {
    wx.navigateTo({
      url: '../regist/regist',
    })
  },

  getUserStatus() {
    //* state=0 没有任何数据，从人脸录入开始
    // * state=1 只是缺少人脸数据
    // * state=2 所有信息都存在，但信息状态不能确定
    // * state=3 群众不能使用该功能
    ajax.get(URI_USERSTATUS, {
      uid: wx.getStorageSync('uid')
    }, {}, res => {
      wx.setStorageSync('dangyuan_id', res.data.dangyaun_id);
      if (res.data.state == 0) {
        wx.reLaunch({
          url: '/pages/submit_info/submit_info',
        })
      } else if (res.data.state == 1) {
        wx.reLaunch({
          url: '/pages/uploadpage/uploadpage'
        })
      } else if (res.data.state == 2) {
        wx.reLaunch({
          url: '/pages/user_manage/user_manage'
        })
      } else if (res.data.state == 3) {
        this.setData({
          show_no_dang_yuan: true
        })
        // wx.showModal({
        //   title:'温馨提示',
        //   content:'您不是党员不能使用该功能！',
        //   showCancel:false,
        //   showConfirm:false
        // })
      }
    })
  },

})